package com.xique.door.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.BuildAreaV1;
import com.xique.door.bean.domain.BuildBuildingV1;
import com.xique.door.bean.domain.BuildElevator;
import com.xique.door.bean.request.buildElevator.BuildElevatorAddRequest;
import com.xique.door.bean.request.buildElevator.BuildElevatorEditRequest;
import com.xique.door.bean.request.buildElevator.BuildElevatorListRequest;
import com.xique.door.bean.search.build.AreaListSearch;
import com.xique.door.bean.search.build.BuildingListSearch;
import com.xique.door.bean.vo.buildElevator.BuildElevatorAllListVO;
import com.xique.door.bean.vo.buildElevator.BuildElevatorListVO;
import com.xique.door.bean.vo.buildElevator.BuildElevatorDetailVO;
import com.xique.door.biz.service.IBuildElevatorBizService;
import com.xique.door.service.IBuildAreaService;
import com.xique.door.service.IBuildBuildingService;
import com.xique.door.service.IBuildElevatorService;
import com.xique.system.api.RemoteSystemUserService;
import com.xique.system.api.dto.OperateDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2022/3/29 11:26
 */
@Service
@Slf4j
public class BuildElevatorBizServiceImpl implements IBuildElevatorBizService {

    @Autowired
    private IBuildElevatorService elevatorService;

    @Autowired
    private IBuildAreaService areaService;

    @Autowired
    private IBuildBuildingService buildingService;

    @Autowired
    private RemoteSystemUserService systemUserService;

    @Override
    public AjaxResult getElevatorList(BuildElevatorListRequest request) {

        request.setParkId(SecurityUtils.getParkId());

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", new ArrayList<>());
        map.put("total", 0);

        IPage<BuildElevator> page = elevatorService.pageList(request);

        if (CollectionUtils.isEmpty(page.getRecords())) {
            return AjaxResult.success(map);
        }

        Map<String, String> transformMap = new HashMap<>(2);
        transformMap.put("updateTime", "operateTime");
        transformMap.put("updateBy", "operateId");
        List<BuildElevatorListVO> vos = OrikaUtil.converts(page.getRecords(), BuildElevatorListVO.class, transformMap);

        List<Long> areaIds = vos.stream().map(BuildElevatorListVO::getAreaId).distinct().collect(Collectors.toList());
        AreaListSearch search = new AreaListSearch();
        search.setIds(areaIds);
        List<BuildAreaV1> areaList = areaService.selectAreaListV1(search);
        Map<Long, String> areaMap = areaList.stream().collect(Collectors.toMap(BuildAreaV1::getId, BuildAreaV1::getAreaName));

        List<Long> buildingIds = vos.stream().map(BuildElevatorListVO::getBuildingId).distinct().collect(Collectors.toList());
        BuildingListSearch buildingListSearch = new BuildingListSearch();
        buildingListSearch.setIds(buildingIds);
        List<BuildBuildingV1> buildingList = buildingService.selectBuildBuildingListV1(buildingListSearch);
        Map<Long, String> buildingMap = buildingList.stream().collect(Collectors.toMap(BuildBuildingV1::getId, BuildBuildingV1::getBuildingName));

        List<Long> operateIds = vos.stream().map(BuildElevatorListVO::getOperateId).distinct().collect(Collectors.toList());
        R<List<OperateDTO>> result = systemUserService.getOperateList(StringUtils.join(operateIds, ","));
        List<OperateDTO> operateList = result.getData();
        Map<Long, String> operateMap = operateList.stream().collect(Collectors.toMap(OperateDTO::getOperateId, OperateDTO::getOperateName));

        vos.forEach(item -> {
            if (StringUtils.isNotNull(areaMap.get(item.getAreaId()))) {
                item.setAreaName(areaMap.get(item.getAreaId()));
            }
            if (StringUtils.isNotNull(buildingMap.get(item.getBuildingId()))) {
                item.setBuildName(buildingMap.get(item.getBuildingId()));
            }
            if (StringUtils.isNotNull(operateMap.get(item.getOperateId()))) {
                item.setOperateName(operateMap.get(item.getOperateId()));
            }
        });

        map.put("list", vos);
        map.put("total", page.getTotal());

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult addElevator(BuildElevatorAddRequest request) {

        BuildElevator elevator = OrikaUtil.convert(request, BuildElevator.class);
        elevator.setParkId(SecurityUtils.getParkId());

        int id = elevatorService.insert(elevator);

        if (id <= 0) {
            return AjaxResult.error("添加失败");
        }

        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult getElevatorDetail(Long id) {

        if (StringUtils.isNull(id)) {
            return AjaxResult.error("id不能为空");
        }

        BuildElevator elevator = elevatorService.getById(id);

        BuildElevatorDetailVO vo = OrikaUtil.convert(elevator, BuildElevatorDetailVO.class);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult editElevator(BuildElevatorEditRequest request) {

        BuildElevator elevator = OrikaUtil.convert(request, BuildElevator.class);
        int id = elevatorService.update(elevator);

        if (id <= 0) {
            return AjaxResult.error("编辑失败");
        }

        return AjaxResult.success("编辑成功");
    }

    @Override
    public AjaxResult removeElevator(Long id) {

        if (StringUtils.isNull(id)) {
            return AjaxResult.error("id不能为空");
        }

        boolean isSuccess = elevatorService.removeById(id);

        if (!isSuccess) {
            return AjaxResult.error("删除失败");
        }

        return AjaxResult.success("删除成功");
    }

    @Override
    public AjaxResult getAllElevatorList(BuildElevatorListRequest request) {

        if (StringUtils.isNull(request.getBuildingId())) {
            return AjaxResult.error("楼栋id不能为空");
        }
        List<BuildElevator> list = elevatorService.list(request);

        List<BuildElevatorAllListVO> vos = OrikaUtil.converts(list, BuildElevatorAllListVO.class);

        Map<String, Object> map = new HashMap<>(1);
        map.put("list", vos);

        return AjaxResult.success(map);
    }
}
